package com.fafa.File.Buffered;

import org.junit.Test;

import java.io.*;
import java.io.FileInputStream;
import java.io.ObjectInputStream;

/**
 * 序列化数据
 */
public class ObjectOutOrWriteStream_ {

    public static void main(String[] args) throws IOException{


    }

    @Test
    public void write() throws IOException {
        String filePath = "E:\\data.dat";
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filePath));

        oos.writeObject(new StoredData("日蚕一样", 3));

        oos.close();
    }


    //read 禁用警告

    @SuppressWarnings("all")
    @Test
    public void read() throws IOException, ClassNotFoundException {
        String filePath = "E:\\data.dat";
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filePath));
        StoredData storedData = (StoredData) ois.readObject();
        System.out.println(storedData);
        ois.close();

    }




















}

class StoredData implements Serializable {
    private String name;
    private int age;

    public StoredData(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
